#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2", "bmi", "bmi2", "popcnt")
#include<bits/stdc++.h>
using namespace std; using ll = long long;
#define For(i, j, k) for ( int i = (j) ; i <= (k) ; i++ )
#define Fol(i, j, k) for ( int i = (j) ; i >= (k) ; i-- )
int n, m, sz, x[8000], y[8000], a[8000], nws[8000], nwb[8000], len, d, u, v, cnt, val[500][65536];
ll ans, ex; bool ok[60009]; unsigned short bss[8000][500], bsb[8000][500], o, oo; short ord[8000][8000];
pair < short, short > f[10000000];
inline bool isprime(int x) { for ( int i = 2 ; i * i <= x ; i++ ) if ( !( x % i ) ) return false; return true; }
inline void init() { For(i, 2, 60000) ok[i] = ( i < 11 || i % 20 == 3 ) && isprime(i); }
inline int dis(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); }
inline void flip(unsigned short *bs, int p) { bs[p >> 4] ^= 1 << ( p & 15 ); }
inline void work()
{
	cin >> n >> m, sz = ( n + 15 ) >> 4, ans = ex = 0;
	For(i, 0, n - 1) cin >> x[i] >> y[i];
	For(i, 0, n - 1) cin >> a[i];
	For(i, 0, sz - 1)
	{
		len = 16; if ( n & 15 && i == sz - 1 ) len = n & 15;
		For(j, 0, len - 1) val[i][1 << j] = a[( i << 4 ) | j];
		For(j, 1, ( 1 << len ) - 1) val[i][j] = val[i][j & -j] + val[i][j ^ ( j & -j )];
	}
	For(i, 0, n - 1)
	{
		fill(bss[i], bss[i] + sz, 0), fill(bsb[i], bsb[i] + sz, 65535), nws[i] = nwb[i] = 0;
		if ( n & 15 ) bsb[i][sz - 1] = ( 1 << ( n & 15 ) ) - 1;
		static short tmp[8000]; static int cnt[256];
		For(j, 0, n - 1) if ( i != j ) ord[i][j - ( j > i )] = j;
		memset(cnt, 0, sizeof(cnt)); For(j, 0, n - 2) cnt[dis(i, ord[i][j]) & 255]++;
		partial_sum(cnt, cnt + 256, cnt); Fol(j, n - 2, 0) tmp[--cnt[dis(i, ord[i][j]) & 255]] = ord[i][j];
		memset(cnt, 0, sizeof(cnt)); For(j, 0, n - 2) cnt[dis(i, tmp[j]) >> 8]++;
		partial_sum(cnt, cnt + 256, cnt); Fol(j, n - 2, 0) ord[i][--cnt[dis(i, tmp[j]) >> 8]] = tmp[j];
	}
	len = 0; For(i, 0, n - 1) For(j, i + 1, n - 1) if ( ok[dis(i, j)] ) f[len++] = make_pair(i, j);
	stable_sort(f, f + len, [&](auto x, auto y) { return dis(x.first, x.second) < dis(y.first, y.second); });
	For(i, 0, len - 1)
	{
		u = f[i].first, v = f[i].second, d = dis(u, v), cnt = 0;
		while ( nws[u] != n - 1 && dis(u, ord[u][nws[u]]) <  d ) flip(bss[u], ord[u][nws[u]++]);
		while ( nwb[u] != n - 1 && dis(u, ord[u][nwb[u]]) <= d ) flip(bsb[u], ord[u][nwb[u]++]);
		while ( nws[v] != n - 1 && dis(v, ord[v][nws[v]]) <  d ) flip(bss[v], ord[v][nws[v]++]);
		while ( nwb[v] != n - 1 && dis(v, ord[v][nwb[v]]) <= d ) flip(bsb[v], ord[v][nwb[v]++]);
		For(j, 0, sz - 1) ans += val[j][o = bss[u][j] & bsb[v][j]] + val[j][oo = bsb[u][j] & bss[v][j]],
						  cnt += __builtin_popcount(( (unsigned)o << 16 ) | oo);
		ans += (ll)( a[u] + a[v] ) * cnt;
	}
	For(i, 0, n - 1) for ( int l = 0, r ; l <= n - 2 ; l = r + 1 )
	{
		for ( d = dis(i, ord[i][l]), r = l ; r < n - 2 && d == dis(i, ord[i][r + 1]) ; r++ );
		if ( ok[d] ) For(j, l, r) For(k, j + 1, r)
			( d != dis(ord[i][j], ord[i][k]) ? ans : ex ) += a[i] + a[ord[i][j]] + a[ord[i][k]];
	}
	cout << ans + ex / 3 << '\n';
}
int main()
{
	freopen("chess.in", "r", stdin), freopen("chess.out", "w", stdout);
	cin.tie(nullptr) -> sync_with_stdio(false);
	int t; init(), cin >> t; while ( t-- ) work(); return 0;
}